  /*  Block Functions:				|
   |						|
   |  The following functions are used to	|
   |   operate on blocks in the given position	*/


    // Function to compute a block's location within the grid
    function compBlockLoc(blocks) {
      var blockLocation = (blocks) * (BLOCK_SIZE + GRID_LINEWIDTH) + GRID_LINEWIDTH + GRID_OFFSET;
      return blockLocation;
    }

    function fillShape(lcolor) {
      canvas.setColor(lcolor,0);
      if (b1y>=0) {
        tetraCan.fillRect(compBlockLoc(b1x), compBlockLoc(b1y), BLOCK_SIZE, BLOCK_SIZE);
        canvas.drawHighLowlight(lcolor, b1x, b1y);
      }
      if (b2y>=0) {
        tetraCan.fillRect(compBlockLoc(b2x), compBlockLoc(b2y), BLOCK_SIZE, BLOCK_SIZE);
        canvas.drawHighLowlight(lcolor, b2x, b2y);
      }
      if (b3y>=0) {
        tetraCan.fillRect(compBlockLoc(b3x), compBlockLoc(b3y), BLOCK_SIZE, BLOCK_SIZE);
        canvas.drawHighLowlight(lcolor, b3x, b3y);
      }
      if (b4y>=0) {
        tetraCan.fillRect(compBlockLoc(b4x), compBlockLoc(b4y), BLOCK_SIZE, BLOCK_SIZE);
        canvas.drawHighLowlight(lcolor, b4x, b4y);
      }
    }
    function occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color) {
      gridCells[b1y][b1x]['occupied']=true;
      gridCells[b1y][b1x]['color']=color;
      gridCells[b2y][b2x]['occupied']=true;
      gridCells[b2y][b2x]['color']=color;
      gridCells[b3y][b3x]['occupied']=true;
      gridCells[b3y][b3x]['color']=color;
      gridCells[b4y][b4x]['occupied']=true;
      gridCells[b4y][b4x]['color']=color;
    }
    function checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y) {
      if (b1y<0) {
        b1y=0;
      }
      if (b2y<0) {
        b2y=0;
      }
      if (b3y<0) {
        b3y=0;
      }
      if (b4y<0) {
        b4y=0;
      }
      try {
        if ((9<b1x<0 || 9<b2x<0 || 9<b3x<0 || 9<b4x<0 || b1y>19 || b2y>19 || b3y>19 || b4y>19)||
          (gridCells[b1y][b1x]['occupied']===true || gridCells[b2y][b2x]['occupied']===true ||
           gridCells[b3y][b3x]['occupied']===true || gridCells[b4y][b4x]['occupied']===true)) {
          var ocu=true
        } else {
          var ocu=false
        }
        return ocu;
      } catch (e) {
        if (e != "TypeError: Cannot read property 'occupied' of undefined") {
          console.error(e);
        }
      }
    }

  // L block
   function Lshape(faces,x,y,action) {
     var color='blue';

     // Determine direction block is facing...
     switch (faces) {
     case 0:
       b1x = b2x = b3x = x;
       b4x = x+1;
       b1y = y;
       b2y = y+1;
       b3y = b4y = y+2;
         break;
     case 1:
       b1x = x+1;
       b2x = x;
       b3x = b4x = x-1;
       b1y = b2y = b3y = y+1;
       b4y = y+2;
         break;
     case 2:
       b1x = b2x = b3x = x;
       b4x = x-1;
       b1y = y+2;
       b2y = y+1;
       b3y = b4y = y;
         break;
     case 3:
       b1x = x-1;
       b2x = x;
       b3x = b4x = x+1;
       b1y = b2y = b3y = y+1;
       b4y = y;
         break;
     }
     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }

  // reverse L blocks
   function L2shape(faces,x,y,action) {
     var color='purple';

     switch (faces) {
     case 0:
       b1x = b2x = b3x = x;
       b4x = x-1;
       b1y = y-1;
       b2y = y;
       b3y = b4y = y+1;
         break;
     case 1:
       b1x = x+1;
       b2x = x;
       b3x = b4x = x-1;
       b1y = b2y = b3y = y;
       b4y = y-1;
         break;
     case 2:
       b1x = b2x = b3x = x;
       b4x = x+1;
       b1y = y+1;
       b2y = y;
       b3y = b4y = y-1;
         break;
     case 3:
       b1x = x-1;
       b2x = x;
       b3x = b4x = x+1;
       b1y = b2y = b3y = y;
       b4y = y+1;
         break;
     }
     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }

  // T blocks
   function Tshape(faces,x,y,action) {
     var color='yellow';

     switch (faces) {
     case 0:
       b1x = x;
       b2x = b4x = x+1;
       b3x = x+2;
       b1y = b2y = b3y = y;
       b4y = y-1;
         break;
     case 1:
       b1x = b2x = b3x = x+1;
       b4x = x+2;
       b1y = y-1;
       b2y = b4y = y;
       b3y = y+1;
         break;
     case 2:
       b1x = x;
       b2x = b4x = x+1;
       b3x = x+2;
       b1y = b2y = b3y = y;
       b4y = y+1;
         break;
     case 3:
       b1x = b2x = b3x = x+1;
       b4x = x;
       b1y = y-1;
       b2y = b4y = y;
       b3y = y+1;
         break;
     }
     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }

  // Z blocks
   function Zshape(faces,x,y,action) {
     var color='green';

     switch (faces) {
       case 0:
         b1x = x-1;
         b2x = b3x = x;
         b4x = x+1;
         b1y = b2y = y;
         b3y = b4y = y+1;
           break;
       case 1:
         b1x = b2x = x+1;
         b3x = b4x = x;
         b1y = y-1;
         b2y = b3y = y;
         b4y = y+1;
           break;
     }
     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }

  // S blocks
   function Sshape(faces,x,y,action) {
     var color='lightBlue';

     switch (faces) {
     case 0:
       b1x = x+1;
       b2x = b3x = x;
       b4x = x-1;
       b1y = b2y = y;
       b3y = b4y = y+1;
         break;
     case 1:
       b1x = b2x = x+1;
       b3x = b4x = x;
       b1y = y+1;
       b2y = b3y = y;
       b4y = y-1;
         break;
     }
     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }

  // O blocks
   function Oshape(faces,x,y,action) {
     var color='red';

     b1x = b3x = x;
     b2x = b4x = x+1;
     b1y = b2y = y;
     b3y = b4y = y-1;

     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }

  // l blocks
   function lshape(faces,x,y,action) {
     var color='orange';
     switch (faces) {
     case 0:
       b1x = b2x = b3x = b4x = x;
       b1y = y+2;
       b2y = y+1;
       b3y = y;
       b4y = y-1;
         break;
     case 1:
       b1x = x+2;
       b2x = x+1;
       b3x = x;
       b4x = x-1;
       b1y = b2y = b3y = b4y = y;
         break;
     }
     if (action=='occupy') {
       occupyShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y, color);
     } else if (action=='check') {
       return checkShape(b1x, b2x, b3x, b4x, b1y, b2y, b3y, b4y);
     }
     fillShape(color);
   }
